/**
 *  *2010-7-26 下午04:41:52  add 
 */
package com.base.myproject.client.HR.attendance;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.HR.Salary.SelectGrid;
import com.base.myproject.client.HR.bcode.HrBcodeSelectButton;
import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 固定的假期类型策略
 * 
 * @author Administrator 2011-3-26 update cxghiu410@gmail.com
 */
public class FixedHolidaysPloy extends BaseJDBCPanel {
	BaseStore<BusiObject> store = null;
	TextArea mark;
	ColumnConfig systembcodecolumn;
	HrBcodeSelectButton hrbsb;
	
	public FixedHolidaysPloy() {
		init();
	}

	public void init() {
		initrenderer();
		initWedgitx();
		registerMainStore(mainstore);
	}
	
	private void initWedgitx() {
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.add(createMGrid());
		VerticalPanel vpanel = new VerticalPanel();
		vpanel.setSpacing(1);
		hpanel.setSpacing(2);
		vpanel.add(createGrid());

		TabPanel panel = new TabPanel();
		panel.setPlain(true);
		panel.setSize(810, 166);
		
		initSelectGrid();
		
		TabItem zz_tab = new TabItem("组织");
		zz_tab.add(createBcodeGrid());
		hrbsb = new HrBcodeSelectButton()
		{
		public void doposted(List<TreeNodeModelData> modeldatalist) {
			for(TreeNodeModelData tnmd : modeldatalist)
			{
				HashMap hm = new HashMap();
				hm.put("systembcode", tnmd.get("systembcode"));
				hm.put("sheetcode", "FixedHolidaysPloy");//这里是针对每个类用到组织条件的时候 添加进去类名  用于区分
				this.getTarget_store().add(hm);
			}
		}
		};
		hrbsb.setCheckNodes(CheckNodes.BOTH);
		hrbsb.setNeesreselect(true);
		hrbsb.setTarget_store(bcode_store);
		zz_tab.setAutoHeight(true);
		panel.add(zz_tab);
		
		TabItem zy_tab = new TabItem("职级");
		zy_tab.addStyleName("pad-text");
		zy_tab.add(zy_Grid);
		panel.add(zy_tab);

		TabItem ji_neng = new TabItem("雇佣组别");// hr_jobgroup
		ji_neng.addStyleName("pad-text");
		ji_neng.add(jobgroup_Grid);
		panel.add(ji_neng);

		TabItem z_j = new TabItem("雇佣性质"); // hr_jobproperty
		z_j.addStyleName("pad-text");
		z_j.add(jobproperty_Grid);
		panel.add(z_j);

		TabItem lb_tab = new TabItem("雇佣类别"); // hr_jobtype
		lb_tab.addStyleName("pad-text");
		lb_tab.add(jobtype_Grid);
		panel.add(lb_tab);

		vpanel.add(panel);
		hpanel.add(vpanel);
		add(hpanel);
	}
	
	SelectGrid zy_Grid;
	SelectGrid jobgroup_Grid;
	SelectGrid jobproperty_Grid;
	SelectGrid jobtype_Grid;
	void initSelectGrid() {
		// 创建多个Grid应用的table页中
		//职级
		zy_Grid = new SelectGrid(
				store,
				"hr_range_zy_c",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'zy' ) as a left join  (select distinct code,isset,upcode from hr_range_zy_c where upcode = '{icode}' and sheetcode='FixedHolidaysPloy' ) as b on a.code = b.code",
				"icode","FixedHolidaysPloy");	
		// 雇佣组别
		jobgroup_Grid = new SelectGrid(
				store,
				"hr_range_jobgroup",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'hr_jobgroup' ) as a left join  (select code,isset,upcode from hr_range_jobgroup where upcode = '{icode}' and sheetcode='FixedHolidaysPloy'  ) as b on a.code = b.code",
				"icode","FixedHolidaysPloy");
		// 雇佣性质
		jobproperty_Grid = new SelectGrid(
				store,
				"hr_range_jobproperty",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'hr_jobproperty' ) as a left join  (select code,isset,upcode from hr_range_jobproperty where upcode = '{icode}' and sheetcode='FixedHolidaysPloy'  ) as b on a.code = b.code",
				"icode","FixedHolidaysPloy");
		// 雇佣类别
		jobtype_Grid = new SelectGrid(
				store,
				"hr_range_jobtype",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'hr_jobtype' ) as a left join  (select code,isset,upcode from hr_range_jobtype where upcode = '{icode}'  and sheetcode='FixedHolidaysPloy' ) as b on a.code = b.code",
				"icode","FixedHolidaysPloy");
		
		zy_Grid.grid.setHeight(125);
		jobgroup_Grid.grid.setHeight(125);
		jobproperty_Grid.grid.setHeight(125);
		jobtype_Grid.grid.setHeight(125);
		
		zy_Grid.setBorders(false);
		jobgroup_Grid.setBorders(false);
		jobproperty_Grid.setBorders(false);
		jobtype_Grid.setBorders(false);
	}
	
	private BaseGridPanel bcodegrid = null;
	private BaseGridPanel createBcodeGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		systembcodecolumn = new ColumnConfig("systembcode", "部门人员", 80);
		columns.add(systembcodecolumn);
		ColumnModel cm = new ColumnModel(columns);
		bcodegrid = new BaseGridPanel(getBcodeStore(), cm, true, true)
		{
			protected void addnewrecord() {
				addrecord();
			}
		};
		cbs.setSelectionMode(SelectionMode.SINGLE);
		bcodegrid.getBg().setSelectionModel(cbs);
		bcodegrid.getBg().setHeight(110);
		bcodegrid.getBg().setBorders(false);
		return bcodegrid;
	}
	
	private BaseStore<BusiObject> bcode_store = null;
	private BaseStore<BusiObject> getBcodeStore() {
		if (bcode_store == null) {
			createBcodeStore();
		}
		return bcode_store;
	}
	
	private BaseStore<BusiObject> createBcodeStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("systembcode");
		type.getField("systembcode").setType(String.class);
		type.addField("upcode");
		type.getField("upcode").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_range_bcode");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_range_bcode where sheetcode='FixedHolidaysPloy' and upcode ='{icode}'");
		 info.setParent(store);// 关联主表
		 info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		 info.setLinkparent_chile_columns(new String[] { "upcode" });//
		// 关联主表字段
		bcode_store = new BaseStore<BusiObject>(type, info);
		return bcode_store;
	}
	
	void addrecord() {
		hrbsb.showEditor();
	}	

	BaseStore<BusiObject> mainstore = null;

	BaseStore<BusiObject> getMStore() {
		if (mainstore == null) {
			createMStore();
		}
		return mainstore;
	}

	BaseStore<BusiObject> createMStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("type");
		type.getField("type").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Attendance_flexHolidays");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select distinct type from hr_Attendance_flexHolidays");
		mainstore = new BaseStore<BusiObject>(type, info){};
		
		return mainstore;
	}

	BaseGrid<BusiObject> maingrid = null;

	private BaseGrid<BusiObject> createMGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		//
	
		ColumnConfig C2 = new ColumnConfig("type", "假期项", 80);
		columns.add(C2);


		ColumnModel cm = new ColumnModel(columns);
		maingrid = new BaseGrid<BusiObject>(getMStore(), cm, false, false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		maingrid.setSelectionModel(cbs);
		maingrid.setHeight(480);
		maingrid.setWidth(200);
		return maingrid;
	}

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("dates");
		type.getField("dates").setType(Integer.class);
		type.addField("type");
		type.getField("type").setType(String.class);
		type.addField("startTime");
		type.getField("startTime").setType(Date.class);
		type.addField("endTime");
		type.getField("endTime").setType(Date.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Attendance_flexHolidays");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_Attendance_flexHolidays where type='{type}'");
		info.setParent(mainstore);// 关联主表
		info.setLinkparentcolumns(new String[] { "type" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "type" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
		
		return store;
	}
	
	BaseGrid<BusiObject> grid = null;
	NumberField C_num3;
	NumberField C_num4;
	NumberField C_num5;
	NumberField C_num6;
	
	private BaseGrid<BusiObject> createGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		
		ColumnConfig C1 = new ColumnConfig("type", "假期名称", 120);
		C1.setEditor(new CellEditor(new TextField<String>()));
		columns.add(C1);
		
		ColumnConfig C5 = new ColumnConfig("dates", "假期天数", 120);
		C5.setNumberFormat(NumberFormat.getFormat("0"));
		C_num5 = new NumberField();
		C_num5.setPropertyEditorType(Integer.class);
		C5.setEditor(new CellEditor(C_num5));
		columns.add(C5);
		
		ColumnConfig C3 = new ColumnConfig("startTime", "开始日期", 120);
		C3.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		DateField startDate = new DateField();
		startDate.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		C3.setEditor(new CellEditor(startDate));
		columns.add(C3);
		
		ColumnConfig C4 = new ColumnConfig("endTime", "结束日期", 120);
		C4.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		DateField endDate = new DateField();
		endDate.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		C4.setEditor(new CellEditor(endDate));
		columns.add(C4);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm, true, true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setHeight(313);
		grid.setWidth(810);
		return grid;
	}

	private void initrenderer()
	{
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
		.create(com.base.myproject.client.tools.GreetingService.class);
greetingService.getDataSet(
		"select systembcode,bname from systembcode_view ",
		new AsyncCallback<DataSet>() {

			@Override
			public void onFailure(Throwable caught) {
				Info.display("", "失败");
			}

			@Override
			public void onSuccess(final DataSet result) {
				 GridCellRenderer gcr = new GridCellRenderer()
				{

					@Override
					public Object render(ModelData model, String property,
							ColumnData config, int rowIndex, int colIndex,
							ListStore store, Grid grid) {
						// TODO Auto-generated method stub
						for(int i=0;i<result.getRowCount();i++)
						{
							
							
							if(result.getValue(i, "systembcode").equals(model.get(property)))
							{
							return	result.getValue(i, "bname");
							}
						}
						return model.get(property);
					
					}
					
				};
				systembcodecolumn.setRenderer(gcr);
				bcodegrid.getBg().getView().refresh(false);
			}

		});
	}
	
	private void command(List<String> lists, final boolean tip) {
		CommandSyncsql csql = new CommandSyncsql();// 执行sql命令
		csql.getV().addAll(lists);
		CommandSyncContainer list = new CommandSyncContainer();// 可以添加的命令
		list.add(csql);

		GreetingServiceAsync greetingService = GWT
				.create(GreetingService.class);
		greetingService.SendCommandSync(list,
				new AsyncCallback<CommandSyncReturnObject>() {
					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("错误", "网络连接失败", null);
					}

					@Override
					public void onSuccess(CommandSyncReturnObject result) {
						if (result.isB()) {

						} else {
							MessageBox.alert("错误", "错误，错误代码"
									+ result.getMessage(), null);

						}
					}
				});
	}
}
